#coding=utf-8
import time
import win32api
import win32con
import os
import autopy
from VK_CODE import VK_CODE


def star_exe(path):
    main = r"start {0}".format(path)
    r_v = os.system(main)
    print r_v


def mousemove_click(x,y):
    autopy.mouse.smooth_move(x, y)
    autopy.mouse.click()


def hello_world():
    autopy.alert.alert("Hello, world")

def where_is_the_window(picture):
    '''查找回收站在屏幕中的位置'''
    rubbish=autopy.bitmap.Bitmap.open(picture)
    screen=autopy.bitmap.capture_screen()
    pos=screen.find_bitmap(rubbish)
    if pos:
        print '找到了，他的位置在:%s' % str(pos)
        return pos
    else:
        print '没有找到'
        return False

def key_input(input_words=''):
    for word in input_words:
        win32api.keybd_event(VK_CODE[word], 0, 0, 0)
        win32api.keybd_event(VK_CODE[word], 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(0.1)


def input_user(user_picture,name):
    while True:
        pos =  where_is_the_window(user_picture)
        if pos:
            mousemove_click(pos[0]+200,pos[1]+10)
            for i in range(10):
                win32api.keybd_event(VK_CODE["backspace"], 0, 0, 0)
                time.sleep(0.2)
            key_input(name)
            break

def click_picture(click_picture):
    pos = where_is_the_window(click_picture)
    mousemove_click(pos[0] +20, pos[1] + 20)


def start():
    path = r"D:\BaiduNetdisk\baidunetdisk.exe"
    user_picture = "user.png"
    pass_picture = "passwd.png"
    login_picture = "login.png"
    name = "test1"
    passwd="123456"
    star_exe(path)
    input_user(user_picture,name)
    input_user(pass_picture,passwd)
    click_picture(login_picture)
start()